home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Watcom: pad a string ?
- Date: 4 Feb 1996 14:30:07 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4f2ftf$rjb@sparcserver.lrz-muenchen.de>
- References: <ZCOExQTC3kJT089yn@pinerolo.gvo.it>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
-
- albe@pinerolo.gvo.it (Alberto Velo) writes:
-
- >Is there a function to pad a string with a charachter, using Watcom C/C++ 10.5
- >?
-
- Maybe, but only the documentation that comes with that implementation of
- the C programming language will tell you for sure.
-
- >I have an array, part of a structure (i.e. msg.from="Myname"); since array is
- >of defined length in the structure ( char from[36] ), I'd need to let msg.from
- >36 chars length, padding it with spaces.
-
- Why do you think so? As long as "msg.from" indeed _is_ a string, i.e. if
- it is '\0'-terminated, there is no need to pad it with spaces. If you
- are dealing with an interface to a system that needs padding, write your
- own padding function or look for an implementation dependend solution.
- The functions memset() and strncpy() from the standard C library might
- be helpful when writing you own padding function.
-
- You could try something like:
-
- #include <string.h>
-
- char *
- pad_str(char *str, int pad, char *tgt, size_t sz)
- {
- size_t len = strlen(str);
- memset(tgt, pad, sz - 1);
- tgt[sz - 1] = '\0';
- return strncpy(tgt, str, len < sz - 1 ? len : sz - 1);
- }
-
- where "str" is the string you want to insert, i.e. "Myname" in your
- example, "pad" is the padding character you want to use, i.e. ' '
- in your example, "tgt" is a target buffer for the padded string,
- i.e "msg.from" in your example, and "sz" is the size of the
- target buffer in bytes.
-
- >Else I need to know how to write the structure to a binary file: if I use a
- >sizeof(structname), while char arrays are shorter than the ones defined in the
- >struct, I find additional garbage chars in the file, who fill the array sizes.
-
- So what? You want to write the whole structure, you write it. Those
- "additional garbare chars" are in the struct, and they appear in the
- file. If you want to avoid storing "garbage" chars, you cannot use
- arrays of characters with a fixed size.
-
- >For example, I fill msg.from with "Myname"; from is defined as 3 chars array,
- >in the struct: when I write the entire struct to the file, the from field
- >appears to be "Myname @ # *
- 3 ..." , that is "Myname" + extra garbage for a
- >total of 36 chars.
-
- How exactly do you fill "msg.from", i.e. do you copy the terminating
- '\0'? I dare say not :-). BTW, is "msg.from" an array of 3 characters
- or an array of 36 characters? In the first case, your problem is that
- "Myname" does not fit into an array of 3 characters.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-